package by.kniazhych.core.view.game.peace {
	
	/**
	 * ...
	 * @author Boutylin Mikhail
	 */
	
		import by.kniazhych.core.data.game.PeaceData;
		import by.kniazhych.core.events.ApplicationEvent;
		import by.kniazhych.core.events.GameEvent;
		import by.kniazhych.core.events.ViewEvent;
		import com.greensock.TweenLite;
		import flash.display.DisplayObject;
		import flash.display.Sprite;
		import flash.display.Stage;
		import flash.events.MouseEvent;
	

	
	[Event(name="peaceReleased", type="by.kniazhych.core.events.GameEvent")]
	public class PeaceView extends Sprite {
		
		//--------------------------------------------------------------------------
		//
		//  CONSTANTS
		//
		//--------------------------------------------------------------------------
		
		static private const LEFT_OFFSET:Number = 60;
		static private const TOP_OFFSET:Number = 60;
		static private const CELL_SIDE_SIZE:Number = 50;
		
		//--------------------------------------------------------------------------
		//
		//  PROPERTIES
		//
		//--------------------------------------------------------------------------
		
		private var _data:PeaceData;
		private var _factory:PeaceViewFactory;
		private var _assets:DisplayObject;
		
		//--------------------------------------------------------------------------
		//
		//  CONSTRUCTOR
		//
		//--------------------------------------------------------------------------
		
		public function PeaceView(data:PeaceData, factory:PeaceViewFactory) {
			super();
			_factory = factory;
			_data = data;
			_data.addEventListener(GameEvent.PEACE_CHANGED, _handlePeaceTypeChanged);
			_data.addEventListener(GameEvent.PEACE_MOVED, _onPeaceMoved);
			_init();
		}
		
		private function _init():void {
			_assets = _factory.createPeace(data.figureType);
			addChild(_assets);
			super.mouseChildren = false;
			super.buttonMode = true;
			super.addEventListener(MouseEvent.MOUSE_DOWN, _handleMouseDown);
		}
		
		//--------------------------------------------------------------------------
		//
		//  PUBLIC METHODS
		//
		//--------------------------------------------------------------------------
		
		public function render(tween:Boolean = true):void {
			var newX:uint =  LEFT_OFFSET + (data.position.x + 0.5) * CELL_SIDE_SIZE;
			var newY:uint = TOP_OFFSET + (data.position.y + 0.5) * CELL_SIDE_SIZE;
			if (tween) {
				TweenLite.to(this, 0.6, { x: newX, y: newY, onComplete:_dispatchRenderingComplete});
			}else {
				this.x = newX;
				this.y = newY;
				_dispatchRenderingComplete();
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  PRIVATE METHODS
		//
		//--------------------------------------------------------------------------
		
		private function _dispatchRenderingComplete():void {
			super.dispatchEvent(new ViewEvent(ViewEvent.RENDERED, true, false));
		}
		
		//--------------------------------------------------------------------------
		//
		//  EVENT HANDLERS
		//
		//--------------------------------------------------------------------------
		
		private function _onPeaceMoved(e:GameEvent):void {
			render();
		}
		
		private function _handlePeaceTypeChanged(e:GameEvent):void {
			removeChild(_assets);
			_assets = _factory.createPeace(data.figureType);
			addChild(_assets);
		}
		
		private function _handleMouseUp(e:MouseEvent):void {
			super.stopDrag();
			Stage(e.currentTarget).removeEventListener(MouseEvent.MOUSE_UP, _handleMouseUp);
			dispatchEvent(new GameEvent(GameEvent.PEACE_RELEASED));
		}
		
		private function _handleMouseDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, _handleMouseUp);
			parent.setChildIndex(this, parent.numChildren - 1);
			super.startDrag(true, parent.getRect(parent));
		}
		
		//--------------------------------------------------------------------------
		//
		//  PROPERTIE ACSESSORS
		//
		//--------------------------------------------------------------------------
		
		public function get data():PeaceData { return _data; }
		
	}

}